--Number of first-class passengers in a given month and year, by country, by age range of passengers.

SELECT country, ageBand, COUNT(*) AS NFirstPassengers
FROM Tickets t JOIN Passenger p ON PassengerFK=PassengerPK JOIN DepartureDate d ON
departureDataPK=departureDataFK
WHERE Class=1 AND Month=200801
GROUP BY country, ageBand 

--Number of different first-class passengers in a given month and year, by country, by age range of passengers.

SELECT country, ageBand, COUNT(DISTINCT PassengerFK) AS NFirstPassengers
FROM Tickets t JOIN Passenger p ON PassengerFK=PassengerPK JOIN DepartureDate d ON
departureDataPK=departureDataFK
WHERE Class=1 AND Month=200801
GROUP BY country, ageBand 


--Number of passengers from Europe to the U.S. in a given month and year, and the total revenue, by country, by age range of passengers.

SELECT country, ageBand, COUNT(*) AS NPassengers,
SUM(t.price) AS TotRevenue
FROM Tickets t JOIN Passenger p ON t.PassengerFK=p.PassengerPK JOIN DepartureDate d ON
t.departureDataPK=d.departureDataFK JOIN
Airport Adep ON t.DepartureAirFK=Adep.AirportPK JOIN  
Airport Ades ON t.DestinationAirFK=Ades.AirportPK
WHERE Month=200801 AND Adep.continent='Europe' AND 
Ades.country='U.S.'
GROUP BY country, ageBand 

--Equivalent

SELECT country, ageBand, COUNT(*) AS NPassengers,
SUM(t.price) AS TotRevenue
FROM Tickets t, Passenger, DepartureDate d, 
Airport Adep, Airport Ades
 WHERE 
t.PassengerFK=p.PassengerPK and
t.departureDataPK=d.departureDataFK and
t.DepartureAirFK=Adep.AirportPK and 
t.DestinationAirFK=Ades.AirportPK and
Month=200801 AND Adep.continent='Europe' AND 
Ades.country='U.S.'
GROUP BY country, ageBand 

--Number of flights, by departure city, by destination city

SELECT Adep.city AS DepartureCity, Ades.city AS DestinationCity, COUNT(DISTINCT t.flightFK) AS NFlights
FROM Tickets t JOIN Airport Adep ON t.DepartureAirFK=Adep.AirportPK 
JOIN Airport Ades ON t.DestinationAirFK=Ades.AirportPK
GROUP BY Adep.city, Ades.city

--Average number of airline passengers, by month, by aircraft type, by country of destination.

SELECT d.month, a.type, Ades.country, count(*)/count(distinct t.flightFK) AS AVGPass
FROM Tickets t JOIN DepartureDate d ON t.departureDateFK= d.departureDatePK JOIN Aircraft a ON
t.aircraftFK=a.aircraftPK JOIN Airport Adep ON
t.DestinationAirFK = Ades.AirportPK
GROUP BY d.month, a.type, Ades.country

--Average number of airline passengers, by class, by holiday date.
SELECT t.class, d.day, count(*)/count(distinct t.flightFK) AS AVGPass
FROM Tickets t JOIN DepartureDate d ON t.departureDateFK= d.departureDatePK 
WHERE d.holidayFlag
GROUP BY t.class, d.day

--Number of flights to airports in Germany from the October to December quarter of a given year, and total management cost of the aircraft, by aircraft type.

SELECT a.type, COUNT(distinct t.flightFK) AS Nflights,
COUNT(DISTINCT d.month) * a.managementCost
FROM Tickets t JOIN DepartureDate d ON t.departureDateFK= d.departureDatePK JOIN Airport Ades ON t.DestinationAirFK = Ades.AirportPK JOIN Aircraft a ON t.aircraftFK=a.aircraftPK 
WHERE Ades.country='Germany' AND d.month IN(200810,200811,200812)
GROUP BY a.type, a.managementCost

--Average profit of all flights, by country of departure, by destination country. The profit of a flight is the total passenger price minus the total flight cost.

WITH Price_cost_flight AS
 (
 SELECT t.flightFK, Ades.country AS DestCountry,    
 Adep.country AS DepCountry, 
 SUM(t.price) AS TotalPrice,
 a.managementCost*Count(distinct d.month) AS FMC,
 a.hourlyCost*COUNT(distinct t.flightFK)*f.duration AS FHC
 FROM Tickets t JOIN Aircraft a ON t.aircraftFK=a.aircraftPK  JOIN Flight f ON t.flightFK=f.flightPK
 JOIN Airport Ades ON Ades.airportPK=t.DestinationAirFK
 JOIN Airport Adep ON Adep.airportPK=t.DepartureAirFK
 JOIN DepartureDate ON d.departureDatePK= t.departureDateFK
 GROUP BY t.flightFK, Ades.country, Adep.country, a.managementCost, f.duration, a.hourlyCost
 )
SELECT DestCountry, DepCountry,
SUM(TotalPrice)-SUM(FMC)-SUM(FHC) AS Profit,
(SUM(TotalPrice)-SUM(FMC)-SUM(FHC))/COUNT(*) AS AVG_Profit
FROM Price_cost_flight
Group by DestCountry, DepCountry


--Total revenue in a given year of flights by month,by destination country.
--The total revenue by month, total revenue by destination country, and the total revenue are also of interest.

SELECT d.month, Ades.country, SUM(price) AS TotalRevenue
FROM Tickets t JOIN DepartureDate ON d.departureDatePK= t.departureDateFK JOIN Airport Ades ON Ades.airportPK=t.DestinationAirFK
WHERE d.year=2008
GROUP BY d.month, Ades.country

SELECT d.month, SUM(price) AS TotalRevenue
FROM Tickets t JOIN DepartureDate ON d.departureDatePK= t.departureDateFK 
WHERE d.year=2008
GROUP BY d.month

SELECT Ades.country, SUM(price) AS TotalRevenue
FROM Tickets t JOIN Airport Ades ON Ades.airportPK=t.DestinationAirFK
WHERE d.year=2008
GROUP BY Ades.country


SELECT SUM(price) AS TotalRevenue
FROM Tickets t 
WHERE d.year=2008


SELECT d.month, Ades.country, SUM(price) AS TotalRevenue
FROM Tickets t JOIN DepartureDate ON d.departureDatePK= t.departureDateFK JOIN Airport Ades ON Ades.airportPK=t.DestinationAirFK
WHERE d.year=2008
GROUP BY CUBE(d.month, Ades.country)

